﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace NotifiableLinqTests.MetaGenerator
{
	public static class TargetFramework
	{
		static readonly TargetDefinition _current;
		static readonly TargetDefinition _obtics = new TargetDefinition { Name = "Obtics", FrameworkPath = @"..\..\_thirdparty\obtics.dll", ObservableEnumerableTypeName = "Obtics.Collections.ObservableEnumerable", AdditionalFiles = new[] { "ConcurrentHashtable.dll" }, ValueProvider="Obtics.Values.IValueProvider" };
		static readonly TargetDefinition _liveLinq = new TargetDefinition { Name = "LiveLinq", FrameworkPath = @"LiveLinq.dll", ObservableEnumerableTypeName = "LiveLinq.ObservableEnumerable", ValueProvider="LiveLinq.IValueProvider"};

		public static Type ObservableEnumerable
		{
			get { return Current.ObservableEnumerable; }
		}

		static TargetFramework()
		{
			_current = _liveLinq;
			// _current = _obtics;
			var bin = Path.GetDirectoryName(typeof(TargetFramework).Assembly.Location);
			var currentFrameworkLibrary = Path.Combine(bin, _current.FrameworkPath);
			if (!File.Exists(currentFrameworkLibrary))
			{
				throw new Exception("Current framework library not found!\r\n" + currentFrameworkLibrary);
			}
			var currentFrameworkLibraryInBin = Path.Combine(bin, Path.GetFileName(_current.FrameworkPath));
			if (!File.Exists(currentFrameworkLibraryInBin))
			{
				File.Copy(currentFrameworkLibrary, currentFrameworkLibraryInBin);

				var path1 = Path.GetDirectoryName(currentFrameworkLibrary);
				var path2 = Path.GetDirectoryName(currentFrameworkLibraryInBin);
				foreach (var file in _current.AdditionalFiles)
				{
					File.Copy(Path.Combine(path1, file), Path.Combine(path2, file), true);
				}
			}
			_current.RuntimeDllPath = currentFrameworkLibraryInBin;
			_current.Initialize();
		}

		public static TargetDefinition Current
		{
			get { return _current; }
		}
	}
}